package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.atguigu.jxc.vo.GoodVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/purchaseListGoods")
public class PurchaseListGoodsController {
    @Autowired
    private PurchaseListGoodsService purchaseListGoodsService;

    @PostMapping("/save")
    @RequiresPermissions("进货管理")
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr){
        this.purchaseListGoodsService.save(purchaseList, purchaseListGoodsStr);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 支付结算（修改进货单付款状态）
     *
     * @param purchaseListId
     * @return
     */
    @PostMapping("/updateState")
//    @RequiresPermissions("")
    public ServiceVO updateState(Integer purchaseListId) {
        this.purchaseListGoodsService.updateState(purchaseListId);
        return new ServiceVO(100, "请求成功");
    }

    /**
     * 进货统计（可根据 商品类别、商品编码或名称 条件查询）
     * @param sTime
     * @param eTime
     * @param goodsTypeId
     * @param codeOrName
     * @return
     */
    @PostMapping("/count")
    public String count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName) throws JsonProcessingException {
        List<GoodVo> goodVos = this.purchaseListGoodsService.count(sTime, eTime, goodsTypeId, codeOrName);
        return MAPPER.writeValueAsString(goodVos);
    }

    @PostMapping("/list")
    @RequiresPermissions("进货管理")
    public List<Map<String,Object>> list(@RequestParam String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime){
        List<Map<String,Object>> mapList = purchaseListGoodsService.list(purchaseNumber,supplierId,state,sTime,eTime);

        return mapList;
    }

    /**
     * 根据进货单id查询进货单商品
     * @param purchaseListId
     * @return
     */
    @PostMapping("/goodsList")
    @RequiresPermissions("进货管理")
    public List<Map<String,Object>> goodsList(@RequestParam Integer purchaseListId){
        List<Map<String,Object>> goodsList = purchaseListGoodsService.goodsList(purchaseListId);
        return goodsList;
    }

    /**
     * 根据进货单id删除进货单
     * @param purchaseListId
     * @return
     */
    @PostMapping("/delete")
    @RequiresPermissions("进货管理")
    public ServiceVO delete(@RequestParam Integer purchaseListId){

        purchaseListGoodsService.delete(purchaseListId);
        return new ServiceVO(100,"请求成功");
    }
}
